package com.eorchis.module.remind.ui.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eorchis.core.service.IBaseService;
import com.eorchis.core.springext.mvc.SessionConstant;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.basedata.domain.BaseData;
import com.eorchis.module.remind.domain.Remind;
import com.eorchis.module.remind.domain.RemindUser;
import com.eorchis.module.remind.service.IRemindService;
import com.eorchis.module.remind.ui.commond.RemindQueryCommond;
import com.eorchis.module.remind.ui.commond.RemindValidCommond;
import com.eorchis.module.security.cas.domain.Department;
import com.eorchis.module.user.domain.User;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: RemindController<br>
 * Description: 提醒表管理控制器<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author RongSL
 * @createDate 2016-05-06
 * @version $Revision$
 */

@SuppressWarnings("unchecked")
@Controller("remindController")
@RequestMapping(value = RemindController.MODULE_PATH)
public class RemindController extends AbstractBaseController<RemindValidCommond,RemindQueryCommond> {
	// 模块页面根路径
	public static final String MODULE_PATH = "/module/remind";
	// 提醒表接口
	@Resource(name = "com.eorchis.module.remind.service.impl.RemindServiceImpl")
	private IRemindService remindService;
	
	@Override
	public IBaseService getService() {
		return remindService;
	}

	@Override
	public String getModulePath() {
		return MODULE_PATH;
	}

	@RequestMapping("/remindList")
	public String remindList(RemindQueryCommond commond,HttpServletRequest request,HttpServletResponse response,ResultState resultState){
		ResultState rs=new ResultState();
		try {
			List<RemindValidCommond> list=(List<RemindValidCommond>) remindService.findList(commond);
			commond.setResultList(list);
			rs.setMessage("查找成功！");
			rs.setState(ResultState.RESULT_STATE_SUCCESS);
		} catch (Exception e) {
			rs.setMessage("缺少ID！");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}finally{
			try {
				BeanUtils.copyProperties(rs, resultState);
			} catch (Exception e) {
			}
		}
		return "";
	}
	
	@RequestMapping("/executeRemind")
	public void executeRemind(){
		for(String  type:Remind.NoSparkRemind){
			try {
				remindService.NoSparkRemind(type);
			} catch (Exception e) {
				System.out.println("无触发点提醒功能出错！");
			}
		}
	     System.out.println("\t\t" + new java.sql.Date(System.currentTimeMillis()).toString());  
	}
	
	public void QuartzRemind(){
		for(String  type:Remind.NoSparkRemind){
			try {
				remindService.NoSparkRemind(type);
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("无触发点提醒功能出错！");
			}
		}
	     System.out.println("\t\t" + new java.sql.Date(System.currentTimeMillis()).toString());  
	}
	
	@RequestMapping("/remind")
	public String remind(RemindValidCommond valid,HttpServletRequest request,HttpServletResponse response,ResultState resultState){
		ResultState rs=new ResultState();
		try {
			RemindQueryCommond commond=new RemindQueryCommond();
			commond.setSearchRemindId(valid.getRemindId());
			//更新读取状态
			remindService.updateRemindState(valid);
			//查询提醒
			RemindValidCommond v=(RemindValidCommond) remindService.findList(commond).get(0);
			valid.setRemindValue(v.getRemindValue());
			valid.setRemindTypeStr(v.getRemindType().getDataName());
			valid.setRemindCategoryStr(v.getRemindCategory().getDataName());
			valid.setCreateDateStr(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(v.getCreateDate()));
			rs.setMessage("查找成功！");
			rs.setState(ResultState.RESULT_STATE_SUCCESS);
		} catch (Exception e) {
			rs.setMessage("缺少ID！");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}finally{
			try {
				BeanUtils.copyProperties(rs, resultState);
			} catch (Exception e) {
			}
		}
		return "";
	}
	
	@RequestMapping("/findBaseDataList")
	public String findBaseDataList(RemindQueryCommond commond,ResultState resultState){
		ResultState rs=new ResultState();
		try{
			List<BaseData> list=remindService.findBaseDataList(commond);
			commond.setResultList(list);
			rs.setMessage("查找成功！");
			rs.setState(ResultState.RESULT_STATE_SUCCESS);
		}catch(Exception e){
			rs.setMessage("查找提醒类型出错！");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}finally{
			try {
				BeanUtils.copyProperties(rs, resultState);
			} catch (Exception e) {
			}
		}
		return "";
	}
	
	@RequestMapping("/updateRemindTypeState")
	public String updateRemindTypeState(RemindQueryCommond commond,ResultState resultState){
		ResultState rs=new ResultState();
		try{
			remindService.updateRemindTypeState(commond);
			if (commond.getSearchRemindState().equals(2))
				remindService.updateRemindTypeS(commond);
			rs.setMessage("更新成功！");
			rs.setState(ResultState.RESULT_STATE_SUCCESS);
		}catch(Exception e){
			rs.setMessage("更新提醒状态出错！");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}finally{
			try {
				BeanUtils.copyProperties(rs, resultState);
			} catch (Exception e) {
			}
		}
		return "";
	}
	@RequestMapping("/updateRemindTypeS")
	public String updateRemindTypeS(RemindQueryCommond commond,ResultState resultState){
		ResultState rs=new ResultState();
		try{
			remindService.updateRemindTypeS(commond);
			rs.setMessage("更新成功！");
			rs.setState(ResultState.RESULT_STATE_SUCCESS);
		}catch(Exception e){
			rs.setMessage("更新提醒状态出错！");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}finally{
			try {
				BeanUtils.copyProperties(rs, resultState);
			} catch (Exception e) {
			}
		}
		return "";
	}
	
	/**
	 * 查询管理员
	 * @Date 2016-05-13
	 * @author RongSL
	 * */
	@RequestMapping("/findAdministrator")
	public  String findAdministrator(RemindQueryCommond query,ResultState resultState,HttpServletRequest request)throws Exception{
		try{
			Department d = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
			User user = (User) request.getSession().getAttribute(SessionConstant.USER_INFO);
			if(!PropertyUtil.objectNotEmpty(query.getDeptId())){
				query.setDeptId(d.getDeptID());
			}
			query.setNoUserID(user.getUserId());
			List<RemindUser> list=remindService.findAdministrator(query);
			query.setResultList(list);
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			resultState.setMessage("系统错误，请刷新页面或联系管理员!");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}
	
	/**
	 * 查询普通学员
	 * @author RongSL
	 * 2018年1月3日
	 * */
	@RequestMapping("/findUserList")
	public  String findUserList(RemindQueryCommond query,ResultState resultState,HttpServletRequest request)throws Exception{
		try{
			Department d = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
			if(!PropertyUtil.objectNotEmpty(query.getDeptId())){
				query.setDeptId(d.getDeptID());
			}
			List<RemindUser> list=remindService.findUserList(query);
			query.setResultList(list);
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			resultState.setMessage("系统错误，请刷新页面或联系管理员!");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}
}
